﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.Configuration;
using System.Reflection;
namespace project2
{
    /// <summary>
    /// 运算对象工厂类
    /// </summary>
    public class OperationFactory
    {
        public static Operation CreateOperate(string operate) //根据传进来的运算符来决定创建什么样的对象
        {
            //读取配置文件
            List<string> OperationChars=new List<string>();         //运算符列表
            List<string> OperationClassNames = new List<string>();   //运算子类列表
            string ops = ConfigurationSettings.AppSettings["operationChar"];
            string[] sa = ops.Split(',');
            for (int i = 0; i < sa.Length; i++)
                OperationChars.Add(sa[i]);
            string cs = ConfigurationSettings.AppSettings["operationClassName"];
            sa = cs.Split(',');
            for (int i = 0; i < sa.Length; i++)
                OperationClassNames.Add(sa[i]);

            Operation oper = null;

            //利用反射和配置文件选择具体运算对象
            Assembly assembly = Assembly.Load("project2");
            oper = (Operation)assembly.CreateInstance("project2." +
                OperationClassNames[OperationChars.IndexOf(operate)]);

            
            return oper;
        }

    }
}
